package com.hainatong.jcmvvmbase.login.fragment;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.jcmvvmbase.BR;
import com.hainatong.jcmvvmbase.Constants;
import com.hainatong.jcmvvmbase.MvvmLazyFragment;
import com.hainatong.jcmvvmbase.R;
import com.hainatong.jcmvvmbase.databinding.FragmentChangePwdBinding;
import com.hainatong.jcmvvmbase.login.viewmodel.PasswordViewModel;
import com.hainatong.jcmvvmbase.utils.ClickUtil;
import com.hainatong.mvvmbase.rxbus.RxBus;
import com.hainatong.mvvmbase.rxbus.RxBusBaseMessage;


public class ChangePwdFragment extends MvvmLazyFragment<FragmentChangePwdBinding, PasswordViewModel>
{
    private boolean isOpenOldPwd = true;

    private boolean isOpenNewPwd = false;

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_change_pwd;
    }

    @Override
    public int getBindingVariable()
    {
        return BR.passwordvm;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
        viewDataBinding.btnSubmit.setOnClickListener( v -> {

            if(StringUtils.isEmpty( viewModel.oldPwd.get() ))
            {
                ToastUtils.showShort("输入密码");
                return;
            }

            if( StringUtils.isEmpty( viewModel.newPwd.get() ) )
            {
                ToastUtils.showShort("输入新密码");
                return;
            }

            if( StringUtils.isEmpty( viewModel.newPwd2.get() ) )
            {
                ToastUtils.showShort("再次输入新密码");
                return;
            }

            if( !viewModel.newPwd.get().trim().equals( viewModel.newPwd2.get().trim() ) )
            {
                ToastUtils.showShort("俩次输入新密码不一致");
                return;
            }


            viewModel.updatePasswOrder().observe(this, result ->{

                if( result )
                {
                    ToastUtils.showShort("密码修改成功");
                    RxBus.getDefault().post(Constants.PWD_SUCCESS_CODE , new RxBusBaseMessage(1,"1"));
                }
                else
                {
                    ToastUtils.showShort("密码修改失败");
                }
            });

        } );
        //设置 明文
        viewDataBinding.etOldPwd.setTransformationMethod( HideReturnsTransformationMethod.getInstance() );

        // 隐藏 就密码
        viewDataBinding.oldPwdShowBtn.setOnClickListener( v -> {
            if (ClickUtil.isFastClick(500, v))
            {
                return;
            }

            isOpenOldPwd = !isOpenOldPwd;
            if (isOpenOldPwd)
            {
                // 明文
                viewDataBinding.etOldPwd.setTransformationMethod(
                        HideReturnsTransformationMethod.getInstance());
                viewDataBinding.oldPwdShowBtn.setImageResource(R.mipmap.ic_eye_open);
            }
            else
            {
                // 显示密文
                viewDataBinding.etOldPwd.setTransformationMethod(
                        PasswordTransformationMethod.getInstance());
                viewDataBinding.oldPwdShowBtn.setImageResource(R.mipmap.ic_eye_close);
            }
        } );


        viewDataBinding.newPwdShowBtn.setOnClickListener( v -> {
            if (ClickUtil.isFastClick(500, v))
            {
                return;
            }
            isOpenNewPwd = !isOpenNewPwd;
            if (isOpenNewPwd)
            {
                // 明文
                viewDataBinding.etNewPwd.setTransformationMethod( HideReturnsTransformationMethod.getInstance());
                viewDataBinding.newPwdShowBtn.setImageResource(R.mipmap.ic_eye_open);
            }
            else
            {
                // 显示密文
                viewDataBinding.etNewPwd.setTransformationMethod( PasswordTransformationMethod.getInstance());
                viewDataBinding.newPwdShowBtn.setImageResource(R.mipmap.ic_eye_close);
            }

        } );

    }

    @Override
    protected void lazyLoad()
    {

    }
}
